package com.dongdong.controller;


import com.dongdong.api.CartApi;
import com.dongdong.api.UserApi;
import com.dongdong.model.TbOrder;
import com.dongdong.model.TbOrderItem;
import com.dongdong.service.OrderService;
import com.dongdong.utils.Result;
import com.dongdong.vo.AddressVO;
import com.dongdong.vo.CartVO;
import com.dongdong.vo.PayVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private UserApi userApi;
    @Autowired
    private CartApi cartApi;

    //    调用用户地址
    @GetMapping("/getAddr")
    public Result getAddr(@RequestHeader String accessToken) {
        List<AddressVO> address = userApi.getAddressByUserId(accessToken);
        return Result.OK().setData(address);
    }

    //    调用当前用户的redis购物车列表
    @GetMapping("/getCartList")
    public Result getCartList(@RequestHeader String accessToken) {
        List<CartVO> cartList = cartApi.getCartList(accessToken);
        return Result.OK().setData(cartList);
    }

    /**
     * 获取订单及订单明细
     *
     * @param accessToken
     * @param addrId
     * @param paymentType
     * @return
     */
    @GetMapping("/addOrderAndOrderItem")
    public Result addOrderAndOrderItem(@RequestHeader String accessToken,
                                       @RequestParam Long addrId, @RequestParam String paymentType) {
        PayVO payVO = orderService.addOrderAndOrderItem(accessToken, addrId, paymentType);
        return Result.OK().setData(payVO);
    }


    @PostMapping("/addAddress")
    public Result addAddress(@RequestHeader String accessToken, @RequestBody AddressVO addressVO) {
        userApi.addAddress(accessToken, addressVO);
        return Result.OK();
    }

    //    支付修改订单信息
    @PostMapping("/updatePayOrder")
    public void updatePayOrder(@RequestBody TbOrder order) {
        orderService.updatePayOrder(order);
    }

    //    支付修改订单明细信息
    @PostMapping("/updatePayOrderItem")
    public void updatePayOrderItem(@RequestBody TbOrderItem item){
        orderService.updatePayOrderItem(item);
    }

    /**
     * 根据订单编号查询order
     * @param outTradeNo
     * @return
     */
    @GetMapping("/selectByOutTradeNo")
    public TbOrder selectByOutTradeNo(@RequestParam String outTradeNo){
        return orderService.selectByOutTradeNo(outTradeNo);
    }

    /**
     * 根据订单编号查询orderItem
     * @param outTradeNo
     * @return
     */
    @GetMapping("/selectByItemOutTradeNo")
    public TbOrderItem selectByItemOutTradeNo(@RequestParam String outTradeNo){
        return orderService.selectByItemOutTradeNo(outTradeNo);
    }

}
